package demo4.controller;

import com.tedu.InputUtil;
import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import demo4.pojo.UserLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;

    public void start() {
        System.out.println("请选择对应的操作，输入序号:");
        System.out.println("1:注册,2:登陆,3:修改用户信息,4:用户列表,5:删除用户");
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt(scanner.nextLine());
        switch (num) {
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updateUser();
                break;
            case 4:
                listUser();
                break;
            case 5:
                deleteUser();
                break;
            default:
                System.out.println("其他功能还在完善中，目前404");
        }
    }

    public void deleteUser(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        if(userMapper.selectByUsername(username) != null){
            int num = userMapper.deleteByUsername(username);
            System.out.println(num > 0 ? "删除成功" : "删除失败");
        }else {
            System.out.println("该用户不存在");
        }
    }

    public void listUser(){
        System.out.println("输出用户列表");
        List<User> users = userMapper.selectAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
    public void updateUser(){
       User user = InputUtil.getInputObject(new User());
       if(userMapper.selectByUsername(user.getUsername()) != null){
           int num = userMapper.update(user);
           System.out.println(num > 0 ? "修改成功" : "修改失败");
       }else {
           System.out.println("该用户不存在");
       }
    }

    public void regUser() {
//        System.out.println("欢迎注册!");
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入用户名:");
//        String username = scanner.nextLine();
//        System.out.println("请输入密码:");
//        String password = scanner.nextLine();
//        System.out.println("请输入昵称:");
//        String nickname = scanner.nextLine();
//        System.out.println("请输入年龄:");
//        int age = scanner.nextInt();
//        User user = new User(null,username,password,nickname,age);
        User user = InputUtil.getInputObject(new User());
        if (userMapper.selectByUsername(user.getUsername()) != null) {
            System.out.println("该用户已经存在了");
        }else{
            int num = userMapper.insert(user);
            System.out.println(num > 0 ? "注册成功" : "注册失败");
        }
    }

    public void loginUser() {
            System.out.println("开始处理用户登陆");
            UserLogin userLogin = InputUtil.getInputObject(new UserLogin());
            User user = userMapper.selectByUsername(userLogin.getUsername());
            if (user != null) {
                if (user.getPassword().equals(userLogin.getPassword())) {
                    System.out.println("登录成功，欢迎【"+user.getNickname()+"】回来");
                } else {
                    System.out.println("账号或密码错误");
                }
            } else {
                System.out.println("该用户不存在，请先注册");
            }



    }








}
